import Crypt from "../../../../utils/Crypt";
import StoreModel from "../../../../models/Store";

const model = new StoreModel()

Page({
  data: {
    page: 0,
    pageSize: 10,
    list: [],
    busy: false,
    type: 1,
    account: {}
  },
  onLoad: function (options) {
    this.setData({ type: options.type })
  },

  onReachBottom() {
    this._getList(true)
  },

  onExtract() {
    wx.navigateTo({
      url: '/sub_service/pages/store/pull/index'
    })
  },

  onShow() {
    this._getList()

    model.getCount().then(response => {
      let { data } = response
      data = Crypt.parse(data)
      this.setData({ account: data.model })
    })
  },

  _getList(append = false) {
    this.setData({
      busy: true,
      page: this.data.page + 1
    })
    model.getList({
      ...this.data,
    }).then(response => {
      let { data } = response
      data = Crypt.parse(data)
      // console.log(data)
      if (data.list.length === 0) {
        this.setData({ busy: true })
      } else {
        this.setData({ busy: false })
        if (append) {
          this.setData({ list: this.data.list.concat(data.list) })
        } else {
          this.setData({ list: data.list })
        }
      }
    })
  },

  handleClick(e) {
    const s = e.currentTarget.dataset.s
    if (s != this.data.type) {
      this.setData({
        type: s,
        page: 0,
        pageSize: 10,
        list: [],
        busy: false,
      })
      this._getList()
    }
  }

});
